from django.db import models
from django.contrib.auth.models import User
from meetings.models import Group


class Task(models.Model):
    """任务信息"""
    PRIORITY_CHOICES = [
        ('low', '低'),
        ('medium', '中'),
        ('high', '高'),
    ]
    
    STATUS_CHOICES = [
        ('todo', '待办'),
        ('doing', '进行中'),
        ('done', '已完成'),
    ]
    
    group = models.ForeignKey(Group, on_delete=models.CASCADE, null=True, blank=True, related_name='tasks', verbose_name='所属小组', db_comment='所属小组ID')
    title = models.CharField(max_length=200, verbose_name='任务标题', db_comment='任务标题')
    description = models.TextField(null=True, blank=True, verbose_name='任务描述', db_comment='任务描述')
    creator = models.ForeignKey(User, on_delete=models.CASCADE, related_name='created_tasks', verbose_name='创建者', db_comment='创建者ID')
    assignee = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, blank=True, related_name='assigned_tasks', verbose_name='负责人', db_comment='负责人 ID')
    priority = models.CharField(max_length=20, choices=PRIORITY_CHOICES, default='medium', verbose_name='优先级', db_comment='优先级:low低,medium中,high高')
    status = models.CharField(max_length=20, choices=STATUS_CHOICES, default='todo', verbose_name='状态', db_comment='状态:todo待办,doing进行中,done已完成')
    start_date = models.DateField(null=True, blank=True, verbose_name='开始日期', db_comment='开始日期')
    due_date = models.DateField(null=True, blank=True, verbose_name='截止日期', db_comment='截止日期')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间', db_comment='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间', db_comment='更新时间')

    class Meta:
        verbose_name = '任务'
        verbose_name_plural = '任务'
        db_table = 'zhahuo_task'
        db_table_comment = '任务信息表'
        ordering = ['-created_at']

    def __str__(self):
        return self.title


class TaskComment(models.Model):
    """任务评论"""
    task = models.ForeignKey(Task, on_delete=models.CASCADE, related_name='comments', verbose_name='任务', db_comment='任务ID')
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='task_comments', verbose_name='评论者', db_comment='评论者ID')
    content = models.TextField(verbose_name='评论内容', db_comment='评论内容')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='评论时间', db_comment='评论时间')

    class Meta:
        verbose_name = '任务评论'
        verbose_name_plural = '任务评论'
        db_table = 'zhahuo_task_comment'
        db_table_comment = '任务评论表'
        ordering = ['created_at']

    def __str__(self):
        return f'{self.user.username}: {self.content[:20]}'


class TaskAttachment(models.Model):
    """任务附件"""
    task = models.ForeignKey(Task, on_delete=models.CASCADE, related_name='attachments', verbose_name='任务', db_comment='任务ID')
    file = models.FileField(upload_to='task_attachments/', verbose_name='文件', db_comment='文件路径')
    file_name = models.CharField(max_length=200, verbose_name='文件名', db_comment='文件名')
    uploaded_by = models.ForeignKey(User, on_delete=models.CASCADE, related_name='uploaded_files', verbose_name='上传者', db_comment='上传者ID')
    uploaded_at = models.DateTimeField(auto_now_add=True, verbose_name='上传时间', db_comment='上传时间')

    class Meta:
        verbose_name = '任务附件'
        verbose_name_plural = '任务附件'
        db_table = 'zhahuo_task_attachment'
        db_table_comment = '任务附件表'

    def __str__(self):
        return self.file_name
